package com.cain.imagefilter;

import android.graphics.Bitmap;
import android.graphics.Matrix;

/**
 * Wrapper class for the Bitmap
 * 
 * @author cainli
 * 
 */
public class FilterImage {

	//原图的bitmap
	private Bitmap image;

	//图片的格式，后缀名（jpg/png）
	private String formatName;

	//图片的大小
	private int width, height;

	//RGB 颜色数组
	protected int[] colourArray;

	public FilterImage(Bitmap img) {
		this.image = img;
		formatName = "jpg";
		width = img.getWidth();
		height = img.getHeight();
		updateColourArray();
	}
	/**
	 * 填充图片色素
	 * @param color
	 */
	public void clearImage(int color) {
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				image.setPixel(x, y, color);
			}
		}
	}

	/**
	 * 填充图片数组colourArray
	 * 在图片初始化的时候调用
	 */
	private void updateColourArray() {
		colourArray = new int[width * height];
		image.getPixels(colourArray, 0, width, 0, 0, width, height);
		int r, g, b;
		for (int y = 0; y < height; y++) {
			for (int x = 0; x < width; x++) {
				int index = y * width + x;
				r = (colourArray[index] >> 16) & 0xff;
				g = (colourArray[index] >> 8) & 0xff;
				b = colourArray[index] & 0xff;
				colourArray[index] = 0xff000000 | (r << 16) | (g << 8) | b;
			}
		}
	}

	/**
	 * 设置某个像素的颜色
	 * @param x
	 * @param y
	 * @param colour
	 */
	public void setPixelColour(int x, int y, int colour) {
		colourArray[((y * image.getWidth() + x))] = colour;
		image.setPixel(x, y, colour);
	}

	/**
	 * 获取某个像素点的颜色
	 * @param x
	 * @param y
	 * @return
	 */
	public int getPixelColour(int x, int y) {
		return colourArray[y * width + x];
	}

	/**
	 * 根据RGB组合 来设置指定像素颜色
	 * @param x
	 * @param y
	 * @param c0
	 * @param c1
	 * @param c2
	 */
	public void setPixelColour(int x, int y, int c0, int c1, int c2) {
		colourArray[((y * image.getWidth() + x))] = (255 << 24) + (c0 << 16)
				+ (c1 << 8) + c2;
		image.setPixel(x, y, colourArray[((y * image.getWidth() + x))]);
	}

	/**
	 * 获取某个像素的R(红色)值 
	 * @param x
	 * @param y
	 * @return
	 */
	public int getRComponent(int x, int y) {
		return (getColourArray()[((y * width + x))] & 0x00FF0000) >>> 16;
	}

	/**
	 * 获取某个像素的G(绿色)值 
	 * @param x
	 * @param y
	 * @return
	 */
	public int getGComponent(int x, int y) {
		return (getColourArray()[((y * width + x))] & 0x0000FF00) >>> 8;
	}

	/**
	 * 获取某个像素的B(蓝色)值 
	 * @param x
	 * @param y
	 * @return
	 */
	public int getBComponent(int x, int y) {
		return (getColourArray()[((y * width + x))] & 0x000000FF);
	}

	/**
	 * 旋转图片
	 * @param rotateDegrees
	 */
	public void rotate(int rotateDegrees) {
		Matrix mtx = new Matrix();
		mtx.postRotate(rotateDegrees);
		image = Bitmap.createBitmap(image, 0, 0, width, height, mtx, true);
		width = image.getWidth();
		height = image.getHeight();
		updateColourArray();
	}
	
	
	public Bitmap getImage() {
		return image;
	}


	public void setImage(Bitmap image) {
		this.image = image;
	}

	/**
	 * 后缀名
	 * @return
	 */
	public String getFormatName() {
		return formatName;
	}
	
	public void setFormatName(String formatName) {
		this.formatName = formatName;
	}

	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	
	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}

	public int[] getColourArray() {
		return colourArray;
	}

	public void setColourArray(int[] colourArray) {
		this.colourArray = colourArray;
	}

}